package com.obas.control {

   import flash.events.EventDispatcher;
   import flash.events.StatusEvent;
   import flash.external.ExternalInterface;

   import mx.core.FlexGlobals;
   import mx.events.DynamicEvent;
   import mx.messaging.ChannelSet;
   import mx.messaging.MultiTopicConsumer;
   import mx.messaging.channels.AMFChannel;
   import mx.messaging.channels.StreamingAMFChannel;
   import mx.messaging.events.MessageEvent;
   import mx.messaging.events.MessageFaultEvent;
   import mx.rpc.AsyncToken;
   import mx.rpc.events.FaultEvent;
   import mx.utils.URLUtil;

   [Bindable]
   [Event(name="message", type="mx.events.DynamicEvent")]
   [Event(name="fault", type="mx.events.DynamicEvent")]
   public class Feed extends EventDispatcher {

      public var messageBrokerURL:String;

      public var channel:String = "messagebroker/streamingamf";

      protected var consumer:MultiTopicConsumer;

      public function Feed() {
      }

      public function subscribe(subtopic:String):void {
         if (consumer) {
            consumer.addSubscription(subtopic);
            consumer.subscribe();
         }
      }

      public function unsubscribe():void {
         if (consumer) {
            consumer.unsubscribe();
            consumer.disconnect();
         }
      }

      public function init():void {
         consumer = new MultiTopicConsumer();
         consumer.destination = "message-feed";
         var amfChannel:AMFChannel = new StreamingAMFChannel();
         amfChannel.uri = getURL();
         var cs:ChannelSet = new ChannelSet();
         cs.addChannel(amfChannel);
         consumer.channelSet = cs;
         consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);
         consumer.addEventListener(FaultEvent.FAULT, consumer_fault);
      }

      protected function messageHandler(event:MessageEvent):void {
         var body:* = event.message.body;
         dispatchMessageEvent("message", body);
      }

      protected function feedManager_fault(event:FaultEvent, token:AsyncToken):void {
         if (event.fault.faultDetail)
            dispatchFaultEvent("fault", event.fault.faultString, event.fault.faultDetail);
      }

      protected function consumer_fault(event:MessageFaultEvent):void {
         if (event.faultDetail)
            dispatchFaultEvent("fault", event.faultString, event.faultDetail);
      }

      public function setConfig(messageBrokerURL:String, channel:String):void {
         if (messageBrokerURL != this.messageBrokerURL || channel != this.channel) {
            this.messageBrokerURL = messageBrokerURL;
            this.channel = channel;
         }
      }

      protected function dispatchFaultEvent(type:String, faultString:String, faultDetail:String):void {
         var e:DynamicEvent = new DynamicEvent(type);
         e.faultString = faultString;
         e.faultDetail = faultDetail;
         dispatchEvent(e);
      }

      protected function dispatchMessageEvent(type:String, spec:*):void {
         var e:DynamicEvent = new DynamicEvent(type);
         e.spec = spec;
         dispatchEvent(e);
      }

      private function getURL():String {
            var contextPath:String = ExternalInterface.call("BrowserHistory.getURL");
            if (URLUtil.isHttpURL(contextPath)) {
               return URLUtil.getFullURL(contextPath, channel)
            } else {
               return "/" + contextPath + "/" + channel;
            }
         }
   }
}